﻿using System;
using System.Security.Cryptography;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

using ApplProtocol;

public class UserInterface : MonoBehaviour
{
    public GameObject dialog;

    public Text outputText;
    public Text connectText;

    public float dialogShowTime;

    public InputField userInputField;
    public InputField passwordInputField;

    public NetworkInterface network;


    private bool isLoginSuccess = false;
    private Dictionary<string, Delegate> map = new Dictionary<string, Delegate>();

    private void Awake()
    {
        map.Add(ApplProtocolType.loginFail.ToString(), new Action(LoginFail));
        map.Add(ApplProtocolType.registFail.ToString(), new Action(RegisterFail));
        map.Add(ApplProtocolType.loginSuccess.ToString(), new Action(LoginSuccess));
        map.Add(ApplProtocolType.registSuccess.ToString(), new Action(RegisterSuccess));

        BtnConnectClicked(); //自动连接
        //userInputField.text = "yim";
        //passwordInputField.text = "123456";
        //BtnLoginOnclicked();
    }

    public void BtnRegsiterOnclicked()
    {
        if (userInputField.text.Length == 0 || userInputField.text.Contains(PclUtils.Separator))
        {
            outputText.GetComponent<Text>().text = "用户名不合法";
            StartCoroutine(ShowTip());
            return;
        }
        if (passwordInputField.text.Length < 6)
        {
            outputText.GetComponent<Text>().text = "密码长度不能小于6个字符";
            StartCoroutine(ShowTip());
            return;
        }

        string userName = userInputField.text;
        string passWord = passwordInputField.text;

        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] passWordMD5Byte = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(passWord)); //MD5 散列

        string passWordMD5 = BitConverter.ToString(passWordMD5Byte).Replace("-", ""); //转换为字符串

        string pclStr = PclUtils.Load(ApplProtocolType.regist, userName, passWordMD5);

        SocketObject.SendString(pclStr);

    }

    public void BtnLoginOnclicked()
    {
        if (userInputField.text.Length == 0 || userInputField.text.Contains(PclUtils.Separator))
        {
            outputText.GetComponent<Text>().text = "用户名不合法";
            StartCoroutine(ShowTip());
            return;
        }

        string userName = userInputField.text;
        string passWord = passwordInputField.text;

        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] passWordMD5Byte = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(passWord)); //MD5 散列

        string passWordMD5 = BitConverter.ToString(passWordMD5Byte).Replace("-", ""); //转换为字符串

        string pclStr = PclUtils.Load(ApplProtocolType.login, userName, passWordMD5);

        SocketObject.SendString(pclStr);
    }

    public void BtnResetInputOnclicked()
    {
        userInputField.text = "";
        passwordInputField.text = "";
    }

    public void BtnConnectClicked()
    {
        SocketObject.ConnectToServer();
    }

    void Update()
    {
        if (!PclObject.handled)
        {
            if(map.ContainsKey(PclObject.type))
            {
                map[PclObject.type].DynamicInvoke();
            }
            //else if()
            //{
            //    connectText.GetComponent<Text>().text = "已连接";
            //}
            PclObject.handled = true;
        }

        //int p = int.Parse("f");
    }

    void RegisterFail()
    {
        outputText.GetComponent<Text>().text = "注册失败，用户已存在";
        StartCoroutine(ShowTip());
    }

    void RegisterSuccess()
    {
        outputText.GetComponent<Text>().text = "注册成功";
        StartCoroutine(ShowTip());
    }

    void LoginFail()
    {
        outputText.GetComponent<Text>().text = "登录失败";

        switch(PclObject.args[0])
        {
            case "1" : outputText.GetComponent<Text>().text += ", 用户已登录"; break;
            case "2" : outputText.GetComponent<Text>().text += ", 密码错误"; break;
            case "3" : outputText.GetComponent<Text>().text += ", 用户不存在";break;
        }

        StartCoroutine(ShowTip());
    }

    void LoginSuccess()
    {
        outputText.GetComponent<Text>().text = "登录成功";
        StartCoroutine(ShowTip());
        Client.userName = userInputField.text;
        Client.state = Client.State.login;
        SceneManager.LoadScene(1);
    }

    IEnumerator ShowTip()
    {
        dialog.gameObject.SetActive(true);

        yield return new WaitForSeconds(dialogShowTime);

        dialog.gameObject.SetActive(false);
    }
}
